package com.luoqiz.utils;

import java.io.UnsupportedEncodingException;

public class DataConvertUtils {

	/**
	 * The long data convert byteArray
	 */
	public static byte[] longToByteArray(long l) {
		byte[] b = new byte[8];
		for (int i = 7; i >= 0; i--) {
			b[7 - i] = (byte) (l >> (i * 8));
		}
		return b;
	}

	/**
	 * The byteArray convert long data
	 */
	public static long byteArrayToLong(byte[] b, int offset) {
		long l0 = (long) (b[offset + 0] & 0xff) << 56;
		long l1 = (long) (b[offset + 1] & 0xff) << 48;
		long l2 = (long) (b[offset + 2] & 0xff) << 40;
		long l3 = (long) (b[offset + 3] & 0xff) << 32;
		long l4 = (long) (b[offset + 4] & 0xff) << 24;
		long l5 = (long) (b[offset + 5] & 0xff) << 16;
		long l6 = (long) (b[offset + 6] & 0xff) << 8;
		long l7 = (long) b[offset + 7] & 0xff;
		return l0 | l1 | l2 | l3 | l4 | l5 | l6 | l7;
	}

	/**
	 * The int data convert byteArray
	 */
	public static byte[] intToByteArray(int intData) {
		byte[] b = new byte[4];
		for (int i = 3; i >= 0; i--) {
			b[3 - i] = (byte) (intData >> (i * 8));
		}
		return b;
	}

	/**
	 * The byteArray convert int data
	 */
	public static int byteArrayToInt(byte[] b, int offset) {
		int i = 0;
		i = i | (b[offset + 0] & 0xff) << 24;
		i = i | (b[offset + 1] & 0xff) << 16;
		i = i | (b[offset + 2] & 0xff) << 8;
		i = i | (b[offset + 3] & 0xff);
		return i;
	}


	private static String bytesToString(byte[] fileNameBytes) {
		String string = null;
		try {
			string = new String(fileNameBytes, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return string;
	}
}
